package com.example.rabbitmq6.listener;

import com.example.rabbitmq6.constant.Constants;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class RetryListener {
//    @RabbitListener(queues = Constants.RETRY_QUEUE)
//    public void listener(Message message, Channel channel) {
//        System.out.println("接收到消息:" + message + channel);
//        //制造出异常使得消费者不能正常处理消息
//        int ret = 3/0;
//        System.out.println("消息处理成功");
//    }

    @RabbitListener(queues = Constants.RETRY_QUEUE)
    public void listener(Message message, Channel channel) throws IOException, InterruptedException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            System.out.println("接收到消息:" + message + "deliveryTag:" + deliveryTag + channel);
            //制造出异常使得消费者不能正常处理消息
            int ret = 3/0;
            System.out.println("消息处理成功");
            channel.basicAck(deliveryTag,false);
        } catch (Exception e) {
            Thread.sleep(1000);
            //第三个参数requeue表示被拒绝的消息是否重新进入队列，true表示重新进入队列并且重新投递这个消息，否则直接丢弃
            channel.basicNack(deliveryTag,false,true);
        }
    }
}
